package com.yu1996.spit.controller;

import com.yu1996.spit.pojo.Spit;
import com.yu1996.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {


    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @return void
     * @description 吐槽点赞
     * @date 2019/10/13 21:30
     */
    @RequestMapping(value = "/thumbup/{spitId}", method = RequestMethod.PUT)
    public Result thumbup(@PathVariable String spitId) {
        //判断是否重复点赞(通过Redis缓存)
        if (redisTemplate.opsForValue().get("thumbup_101") != null) {
            return new Result(false, StatusCode.REPERROR, "您已经点过赞了");
        }
        spitService.thumbup(spitId);
        redisTemplate.opsForValue().set("thumbup_101", 1);
        return new Result(true, StatusCode.OK, "点赞成功");
    }


    /**
     * @param parentid
     * @param page
     * @param size
     * @return entity.Result
     * @description 根据上级ID查询吐槽数据（分页）
     * @date 2019/10/13 21:24
     */
    @RequestMapping(value = "/comment/{parentid}/{page}/{size}", method = RequestMethod.GET)
    public Result findByParentId(@PathVariable String parentid, @PathVariable int page, @PathVariable int size) {
        Page<Spit> spits = spitService.findByParentId(parentid, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<>(spits.getTotalElements(), spits.getContent()));
    }


    /**
     * @description 查询所有
     * @date 2019/10/13 20:15
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Spit> all = spitService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", all);
    }

    /**
     * @description 根据id查询
     * @date 2019/10/13 20:15
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.GET)
    public Result findById(@PathVariable String spitId) {
        Spit spit = spitService.findById(spitId);
        return new Result(true, StatusCode.OK, "查询成功", spit);
    }

    /**
     * @description 添加
     * @date 2019/10/13 20:16
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Spit spit) {
        spitService.add(spit);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /**
     * @description 删除
     * @date 2019/10/13 20:16
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String spitId) {
        spitService.delete(spitId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * @description 修改
     * @date 2019/10/13 20:16
     */
    @RequestMapping(value = "/{spitId}", method = RequestMethod.PUT)
    public Result update(@RequestBody Spit spit, @PathVariable String spitId) {
        spit.set_id(spitId);
        spitService.update(spit);
        return new Result(true, StatusCode.OK, "修改成功");
    }
}
